﻿using DevExpress.XtraTreeList;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

namespace Git
{
    partial class git
    {

        //分支信息
        public static List<gitconfig> fetchcodelist = new List<gitconfig>();

        //git 配置
        public static List<gitconfig> gitexelist = new List<gitconfig>();


        /// <summary>
        /// 加载分支
        /// </summary>
        public static void LoadToTree(TreeList tl)
        {
            string thisExeDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string thisDirectory = thisExeDirectory + @"JSONGitConfig";
            if (!Directory.Exists(thisDirectory))
            {
                Directory.CreateDirectory(thisDirectory);
            }
            string thisFile = thisDirectory + "\\GitConfig.json";
            if (File.Exists(thisFile))
            {
                using (StreamReader file = File.OpenText(thisFile))
                {
                    Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                    fetchcodelist = (List<gitconfig>)serializer.Deserialize(file, typeof(List<gitconfig>));
                }
                if (tl != null)
                {
                    tl.DataSource = fetchcodelist;
                    tl.RefreshDataSource();
                }
            }

            string thisFile2 = thisDirectory + "\\GitEXEConfig.json";
            if (File.Exists(thisFile2))
            {
                using (StreamReader file2 = File.OpenText(thisFile2))
                {
                    Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                    gitexelist = (List<gitconfig>)serializer.Deserialize(file2, typeof(List<gitconfig>));
                }
            }
        }

     
        public static void Save()
        {

            string thisExeDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string thisFile = thisExeDirectory + @"JSONGitConfig\\" + "GitConfig.json"; ;
            using (StreamWriter file = File.CreateText(thisFile))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(file, fetchcodelist);
            }

            string thisFile2 = thisExeDirectory + @"JSONGitConfig\\" + "GitEXEConfig.json"; ;
            using (StreamWriter file2 = File.CreateText(thisFile2))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(file2, gitexelist);
            }
        }
    }

    public class gitconfig : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _Key;
        public string Key
        {
            get { return _Key; }
            set
            {
                _Key = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Key"));
                }
            }
        }

        private string _Value;

        public string Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Value"));
                }
            }
        }

        private string _Branch;

        public string Branch
        {
            get { return _Branch; }
            set
            {
                _Branch = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Branch"));
                }
            }
        }


        #region 节点目录结构一定要放置到最后

        /// <summary>
        /// 当前节点ID
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 父节点ID
        /// </summary>
        public string ParentID { get; set; }

        #endregion
    }
}
